"""
1. 获取目录
2. 请求每个目录章节的内容
3. 提取主要内容，进行拼接
    使用html处理内容，参考 notes/使用lxml.html写文档.md
4. 保存
"""
from os.path import join

import requests
from lxml import html
from selenium import webdriver

from main.config import driver_path, chrome_options, path
from utils.image_util import download_save_img

browser = webdriver.Chrome(executable_path=driver_path, options=chrome_options)
browser.implicitly_wait(5)


def download(url):
    browser.get(url)
    book_name = browser.find_element_by_css_selector('div.tab > span').text
    catalogs_tab = browser.find_elements_by_css_selector('#leftcolumn > a')

    catalogs = []  # (name, href)
    for catalog_tab in catalogs_tab:
        catalogs.append((catalog_tab.text, catalog_tab.get_attribute('href')))

    doc = html.document_fromstring('''
    <html>
    <head>
        <link rel="stylesheet" href="http://www.runoob.com/wp-content/themes/runoob/style.css?v=1.170" type="text/css" media="all">
    </head>
    
    <body style="
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translate(-50%);
    width: 50%;
    height: 100%;
    ">
    </body>
    </html>''')
    body = doc.xpath('//body')[0]
    for catalog in catalogs:
        article_tab = None
        response = requests.get(catalog[1])
        if response.status_code == 200:
            article_tab = html.fromstring(response.text).xpath('//*[@id="content"]')[0]
            img_tabs = article_tab.findall('.//img')
            for img_tab in img_tabs:
                img_src = img_tab.get('src').replace('https:', '').replace('//www.runoob.com/', '')
                img_name = img_tab.get('src').split('/')[-1]
                download_save_img('https://www.runoob.com/{}'.format(img_src), join(book_name, img_name))
                img_tab.attrib['src'] = './{}/{}'.format(book_name, img_name)
        
        body.append(article_tab)
        body.append(html.fromstring('<br><br><br>'))

    with open('{}/{}.html'.format(path, book_name), 'wb') as f:
        f.write(html.tostring(doc))
        print('完成')


if __name__ == '__main__':
    name = 'design-pattern'
    prefix = 'https://www.runoob.com/'
    download(prefix + name)
